﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace MyMusicPlay._4._0.Operate
{
    public static class Animate
    {
        public enum direction
        {
            Width, Height
        }

        public static void Move(FrameworkElement sp, Thickness Old, Thickness New, double Milliseconds, Action<ThicknessAnimation>? action = null)
        {
            if (sp == null)
                return;
            Storyboard sb = new Storyboard();
            ThicknessAnimation ta = new ThicknessAnimation();
            ta.From = Old;
            ta.To = New;
            ta.Duration = TimeSpan.FromMilliseconds(Milliseconds);
            if (action is not null)
                action(ta);
            sb.Children.Add(ta);
            Storyboard.SetTarget(ta, sp);
            Storyboard.SetTargetProperty(ta, new PropertyPath("Margin"));
            sb.Begin();
        }
        public static void Deformation(FrameworkElement sp, direction WH, double Old, double New, double Milliseconds, Action<DoubleAnimation>? action = null)
        {
            if (sp == null)
                return;
            Storyboard sb = new Storyboard();
            DoubleAnimation ta = new DoubleAnimation();
            ta.From = Old;
            ta.To = New;
            ta.Duration = TimeSpan.FromMilliseconds(Milliseconds);
            if (action is not null)
                action(ta);
            sb.Children.Add(ta);
            Storyboard.SetTarget(ta, sp);
            Storyboard.SetTargetProperty(ta, new PropertyPath(WH.ToString()));
            sb.Begin(sp, true);
        }

        public static void Move_IsStop(Storyboard sb, FrameworkElement sp, Thickness Old, Thickness New, double Milliseconds, Action<ThicknessAnimation>? action = null)
        {
            if (sp == null)
                return;
            if (Milliseconds < 0)
                return;
            ThicknessAnimation ta = new ThicknessAnimation();
            ta.From = Old;
            ta.To = New;
            ta.Duration = TimeSpan.FromMilliseconds(Milliseconds);
            if (action is not null)
                action(ta);
            sb.Children.Add(ta);
            Storyboard.SetTarget(ta, sp);
            Storyboard.SetTargetProperty(ta, new PropertyPath("Margin"));
            sb.Begin(sp, true);
        }
        public static void Deformation_IsStop(Storyboard sb, FrameworkElement sp, direction WH, double Old, double New, double Milliseconds, Action<DoubleAnimation>? action = null)
        {
            if (sp == null)
                return;
            DoubleAnimation ta = new DoubleAnimation();
            ta.From = Old;
            ta.To = New;
            ta.Duration = TimeSpan.FromMilliseconds(Milliseconds);
            if (action is not null)
                action(ta);
            sb.Children.Add(ta);
            Storyboard.SetTarget(ta, sp);
            Storyboard.SetTargetProperty(ta, new PropertyPath(WH.ToString()));
            sb.Begin(sp, true);
        }

        public static void Variation(Grid grid, int index, direction WH, GridLength Old, GridLength New, double Milliseconds, Action<GridLengthAnimation>? action = null)
        {
            GridLengthAnimation ta = new GridLengthAnimation();
            ta.From = Old;
            ta.To = New;
            ta.Duration = TimeSpan.FromMilliseconds(Milliseconds);
            if (action is not null)
                action(ta);
            if (WH == direction.Height)
                grid.RowDefinitions[index].BeginAnimation(RowDefinition.HeightProperty, ta);
            else if (WH == direction.Width)
                grid.ColumnDefinitions[index].BeginAnimation(ColumnDefinition.WidthProperty, ta);
        }

        /// <summary>
        /// 暂停动画[保持原位置]
        /// </summary>
        public static void FixedElement(Storyboard sb, StackPanel sp)
        {
            var oldMargin = sp.Margin;
            sb.Remove(sp);
            sp.Margin = new Thickness(oldMargin.Left, oldMargin.Top, oldMargin.Right, oldMargin.Bottom);
        }
    }
}
